﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

[assembly: WebResource("ExtJSExtender.Controls.Button.ButtonExtenderBehavior.js", "text/javascript")]

namespace ExtJSExtender.Controls
{
    [ToolboxBitmap(typeof(System.Web.UI.WebControls.Button))]
    public class Button : ExtJSControlBase, IPostBackEventHandler, INamingContainer
    {
        #region 属性

        /// <summary>
        /// 显示的文本
        /// </summary>
        [ExtJSExtender.DescribableProperty("Text")]
        public string Text
        {
            get
            {
                if (ViewState["Text"] == null)
                    ViewState["Text"] = string.Empty;
                return (string)ViewState["Text"];
            }
            set
            {
                ViewState["Text"] = value;
            }
        }

        /// <summary>
        /// 用户点击以后要提示的文本
        /// </summary>
        [ExtJSExtender.DescribableProperty("Message")]
        public string Message
        {
            get
            {
                if (ViewState["Message"] == null)
                    ViewState["Message"] = string.Empty;
                return (string)ViewState["Message"];
            }
            set
            {
                ViewState["Message"] = value;
            }
        }

        /// <summary>
        /// 点击后是否禁用：防止重复提交
        /// </summary>
        [ExtJSExtender.DescribableProperty("DisableAfterClick")]
        public bool DisableAfterClick
        {
            get
            {
                if (ViewState["DisableAfterClick"] == null)
                    ViewState["DisableAfterClick"] = true;
                return (bool)ViewState["DisableAfterClick"];
            }
            set
            {
                ViewState["DisableAfterClick"] = value;
            }
        }

        /// <summary>
        /// 客户端点击事件
        /// </summary>
        [ExtJSExtender.DescribableProperty("OnClientClick")]
        public string OnClientClick
        {
            get
            {
                if (ViewState["OnClientClick"] == null)
                    ViewState["OnClientClick"] = string.Empty;
                return (string)ViewState["OnClientClick"];
            }
            set
            {
                ViewState["OnClientClick"] = value;
            }
        }

        /// <summary>
        /// 边框的宽度
        /// </summary>
        public Unit Border
        {
            get
            {
                if (ViewState["Border"] == null)
                    ViewState["Border"] = new Unit(1);
                return (Unit)ViewState["Border"];
            }
            set
            {
                ViewState["Border"] = value;
            }
        }

        /// <summary>
        /// 是否触发验证
        /// </summary>
        [ExtJSExtender.DescribableProperty("CausesValidation")]
        public bool CausesValidation
        {
            get
            {
                if (ViewState["CausesValidation"] == null)
                    ViewState["CausesValidation"] = true;

                return (bool)ViewState["CausesValidation"];
            }
            set
            {
                ViewState["CausesValidation"] = value;
            }
        }

        [ExtJSExtender.DescribableProperty("TriggerPostBackEvents")]
        [PersistenceMode(PersistenceMode.InnerProperty), Bindable(true), Localizable(true)]
        public ButtonTriggerPostBackEvents TriggerPostBackEvents
        {
            get
            {
                if (ViewState["TriggerPostBackEvents"] == null)
                    ViewState["TriggerPostBackEvents"] = new ButtonTriggerPostBackEvents();

                return (ButtonTriggerPostBackEvents)ViewState["TriggerPostBackEvents"];
            }
            set
            {
                ViewState["TriggerPostBackEvents"] = value;
            }
        }

        #endregion

        #region 构造函数

        public Button()
            : base(HtmlTextWriterTag.Div)
        {

        }

        #endregion

        #region 事件

        public event EventHandler Click;

        #endregion

        #region ExtScriptWebControlBase 成员

        public override IEnumerable<ScriptDescriptor> GetScriptDescriptors()
        {
            ScriptControlDescriptor descriptor = new ScriptControlDescriptor("ExtJSExtender.Controls.ButtonExtenderBehavior", this.ClientID);

            descriptor.AddProperty("Enabled", Enabled);
            descriptor.AddProperty("Width", this.Width.Value);
            descriptor.AddProperty("Height", this.Height.Value);
            descriptor.AddProperty("Border", this.Border.Value);
            descriptor.AddProperty("UniqueID", this.UniqueID);

            ExtJSExtender.Util.DescribeProperties(this, descriptor);

            var descriptors = base.GetScriptDescriptors().ToList();
            descriptors.Add(descriptor);
            return descriptors;
        }

        public override IEnumerable<ScriptReference> GetScriptReferences()
        {
            var reference = new ScriptReference();
            reference.Assembly = "ExtJSExtender.Controls";
            reference.Name = "ExtJSExtender.Controls.Button.ButtonExtenderBehavior.js";

            var references = base.GetScriptReferences().ToList();
            references.Add(reference);
            return references;
        }

        #endregion

        #region IPostBackEventHandler 成员

        public void RaisePostBackEvent(string eventArgument)
        {
            switch (eventArgument.ToLower())
            {
                case "click":
                    {
                        if (Click != null) Click(this, null);
                    }
                    break;
            }
        }

        #endregion
    }
}
